﻿using System.Web.Mvc;
using System.Web.Routing;

namespace CourseSystem.Common.Helpers
{
    /// <summary>
    /// Html扩展方法
    /// </summary>
    public static class HtmlExtensions
    {
        public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper, string linkText, string actionName)
        {
            return ActionButton(htmlHelper, linkText, actionName, null);
        }


        public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
        {
            return ActionButton(htmlHelper, linkText, actionName, controllerName, null, null);
        }

        public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues)
        {
            return ActionButton(htmlHelper, linkText, actionName, controllerName, routeValues, null);
        }

        public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues)
        {
            return ActionButton(htmlHelper, linkText, actionName, null, routeValues, null);
        }

        public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
        {
            return ActionButton(htmlHelper, linkText, actionName, null, routeValues, htmlAttributes);
        }



        public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
        {
            var builder = new TagBuilder("input");
            builder.MergeAttribute("type", "button");
            builder.MergeAttribute("value", linkText);
            //添加属性            
            var dic = new RouteValueDictionary(htmlAttributes);
            builder.MergeAttributes(dic);
            object confirmStr;
            object isParent = false;
            var location = "window.location.href='";
            object isNewWindows;
            bool isNew = false;
            if (dic.TryGetValue("isNewWindows", out isNewWindows))
            {
                isNew = true;
                location = "window.open('";
            }
            else
                if (dic.TryGetValue("isParent", out isParent))
                {
                    location = "parent." + location;
                }
            var onclickStr = location
                             + UrlHelper.GenerateUrl(null, actionName, controllerName,
                                                     new RouteValueDictionary(routeValues), htmlHelper.RouteCollection,
                                                     htmlHelper.ViewContext.RequestContext, true) + "'" + (isNew ? ")" : "");
            if (dic.TryGetValue("confirm", out confirmStr))
            {
                onclickStr = "if(!" + confirmStr + ")return;" + onclickStr;
                builder.Attributes.Remove("confirm");
            }
            builder.MergeAttribute("onclick", onclickStr
                                 , true);
            //输出控件
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        }
    }
}
